package com.practices2.base;

import android.app.Application;

import com.practices2.util.cache.TAFileCache;
import com.practices2.util.cache.TAFileCache.TACacheParams;
import com.practices2.util.config.TAIConfig;
import com.practices2.util.config.TAPreferenceConfig;
import com.practices2.util.config.TAPropertiesConfig;

public class TAApplication extends Application {

	private static TAApplication mApplication;
	/** ThinkAndroid 文件缓存 */
	private TAFileCache mFileCache;

	/** 配置器 */
	private TAIConfig mCurrentConfig;
	/** 配置器 为Preference */
	public final static int PREFERENCE_CONFIG = 0;
	/** 配置器 为PROPERTIESCONFIG */
	public final static int PROPERTIES_CONFIG = 1;

	private static final String SYSTEMCACHE = "com.practices2";

	public static TAApplication getApplication() {
		if (mApplication != null) {
			return mApplication;
		}
		return new TAApplication();
	}

	public void setFileCache(TAFileCache mFileCache) {
		this.mFileCache = mFileCache;
	}

	public TAFileCache getFileCache() {
		if (mFileCache == null) {
			TACacheParams cacheParams = new TACacheParams(this, SYSTEMCACHE);
			TAFileCache fileCache = new TAFileCache(cacheParams);
			getApplication().setFileCache(fileCache);
		}
		return mFileCache;
	}

	public TAIConfig getConfig(int confingType) {

		if (confingType == PREFERENCE_CONFIG) {
			mCurrentConfig = TAPreferenceConfig.getPreferenceConfig(this);
		} else if (confingType == PROPERTIES_CONFIG) {
			mCurrentConfig = TAPropertiesConfig.getPropertiesConfig(this);
		} else {
			mCurrentConfig = TAPreferenceConfig.getPreferenceConfig(this);
		}

		if (!mCurrentConfig.isLoadConfig()) {
			mCurrentConfig.loadConfig();
		}

		return mCurrentConfig;
	}

	public TAIConfig getCurrentConfig() {
		if (mCurrentConfig == null) {
			getPreferenceConfig();
		}
		return mCurrentConfig;
	}

	public TAIConfig getPreferenceConfig() {
		return getConfig(PREFERENCE_CONFIG);
	}

}
